package com.rxmvp.rxmvpdemo.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.rxmvp.rxmvpdemo.App;
import com.rxmvp.rxmvpdemo.R;
import com.rxmvp.rxmvpdemo.base.mvp.BasePresenterActivity;
import com.rxmvp.rxmvpdemo.contract.main.DaggerMainComponent;
import com.rxmvp.rxmvpdemo.contract.main.MainContract;
import com.rxmvp.rxmvpdemo.contract.main.MainModule;
import com.rxmvp.rxmvpdemo.contract.main.MainPresenter;

import butterknife.BindView;

/**
 * 主页面
 *
 * 作者： huangRZ on 2017/1/21. 15:04
 * 邮箱：917647409@qq.com
 */
public class MainActivity extends BasePresenterActivity<MainPresenter> implements MainContract.IView {

    @BindView(R.id.tv_json)
    TextView tv;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DaggerMainComponent.builder()
                .mainModule(new MainModule(this, this))
                .globalComponent(App.getGlobalComponent())
                .build().inject(this);

        //利用leakCanary检测是否发生内存泄漏
        App.getRefWatcher(this).watch(this);
    }

    //取消异步操作
    public void cancel(View view) {
        mPresenter.cancelSubscription();
    }

    //获取网络数据
    public void getData(View view) {
        mPresenter.loadData();
    }

    @Override
    public void setText(String text) {
        tv.setText(text);
    }

    @Override
    public void showPB() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hidePB() {
        progressBar.setVisibility(View.GONE);
    }

    //跳转到second页面
    public void intoRxBus(View view) {
        startActivity(new Intent(this, SecondActivity.class));
    }

}
